package me.zhengjie.util;

import me.zhengjie.entity.QueryRecord;
import me.zhengjie.service.mapper.QueryMapper;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import static me.zhengjie.util.PathUtil.getResultFilePath;

public class DbUtil {
    private DbUtil() {
    }

    public static void storeError(QueryMapper queryMapper, String dataSource, String engine, String queryId, String query, String user, String errorMessage) {
        try {

            ZonedDateTime submitTimeZdt = TimeUtil.convertToZonedDateTime(queryId);
            ZonedDateTime now = ZonedDateTime.now();
            String fetchResultTimeString = TimeUtil.convertZonedDateTimeToString(now);
            long elapsedTimeMillis = Math.abs(ChronoUnit.MILLIS.between(now,submitTimeZdt));

            Path dst = getResultFilePath(dataSource, queryId, true);
            try (BufferedWriter bw = Files.newBufferedWriter(dst, StandardCharsets.UTF_8)) {
                bw.write(errorMessage);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            long resultFileSize = Files.size(dst);
            insert(queryMapper, dataSource, engine, queryId, fetchResultTimeString, user, elapsedTimeMillis, resultFileSize, query, null, Status.FAILED.name());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void insertQueryHistory(QueryMapper queryMapper, String dataSource, String engine, String query, String user, String queryId, Long linenumber) {
        try {
            ZonedDateTime submitTimeZdt = TimeUtil.convertToZonedDateTime(queryId);
            ZonedDateTime now = ZonedDateTime.now();
            String fetchResultTimeString = TimeUtil.convertZonedDateTimeToString(now);
            long elapsedTimeMillis = Math.abs(ChronoUnit.MILLIS.between(now,submitTimeZdt));
            Path resultFilePath = PathUtil.getResultFilePath(dataSource, queryId, false);
            long resultFileSize = Files.size(resultFilePath);
            insert(queryMapper, dataSource, engine, queryId, fetchResultTimeString, user, elapsedTimeMillis, resultFileSize, query, linenumber, Status.SUCCEED.name());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static void insert(QueryMapper queryMapper, String dataSource, String engine, String queryId, String fetchResultTimeString, String user, Long elapsedTimeMillis, Long resultFileSize, String queryString, Long lineNum, String status) {
        QueryRecord params = new QueryRecord();
        params.setDataSource(dataSource);
        params.setEngineName(engine);
        params.setQueryId(queryId);
        params.setFetchResultTimeString(fetchResultTimeString);
        params.setQuerySql(queryString);
        params.setUserName(user);
        params.setStatus(status);
        params.setElapsedTimeMillis(elapsedTimeMillis);
        params.setResultFileSize(resultFileSize);
        if (lineNum != null) params.setLineNumber(lineNum);
        queryMapper.insert(params);
    }
}
